package com.aaron.kjviewutils;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aaron.kjviewutils.glide.WebpDataModel;
import com.bumptech.glide.Glide;

import java.util.Collections;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：Aaron on 2017/2/10 15:55
 * 邮箱：kaijin.fang@edianniu.com
 */
public class MainRecyclerViewAdapter extends RecyclerView.Adapter<MainRecyclerViewAdapter.MainRecyclerViewHolder> implements ItemTouchMoveListener {

    private Context context;
    private List<MainBean.MainSub> datas;
    private OnItemClickListener mOnItemClickListener;

    public MainRecyclerViewAdapter(Context context, List<MainBean.MainSub> datas)
    {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public MainRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
        return new MainRecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MainRecyclerViewHolder holder, int position) {
        if(null!=datas&&!datas.isEmpty())
        {
            MainBean.MainSub mainSub = datas.get(position);
            Glide.with(context).load(new WebpDataModel("http://img4.imgtn.bdimg.com/it/u=948149305,3170847453&fm=21&gp=0.jpg")).placeholder(R.mipmap.ic_launcher).into(holder.logoView);
            holder.title.setText(mainSub.city);
            holder.content.setText(mainSub.temperature);
            StringBuilder builder = new StringBuilder();
            builder.append("今天是");
            builder.append(mainSub.week);
            builder.append("，");
            builder.append("今天");
            builder.append(mainSub.exerciseIndex);
            builder.append("运动");
            builder.append("，");
            builder.append("适合穿");
            builder.append(mainSub.dressingIndex);
            builder.append("。");
            holder.desc.setText(builder.toString());
            if(null!=mOnItemClickListener)
            {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickListener.onItemClick(v, holder.getLayoutPosition());
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return (null==datas)?0:datas.size();
    }

    @Override
    public boolean onItemMove(int fromPosition, int toPosition) {
        Collections.swap(datas, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        return true;
    }

    @Override
    public boolean onItemRemove(int position) {
        datas.remove(position);
        notifyItemRemoved(position);
        return true;
    }

    public static class MainRecyclerViewHolder extends RecyclerView.ViewHolder
    {
        @Bind(R.id.cards)
        RelativeLayout cards;
        @Bind(R.id.logo)
        ImageView logoView;
        @Bind(R.id.title)
        TextView title;
        @Bind(R.id.content)
        TextView content;
        @Bind(R.id.desc)
        TextView desc;

        public MainRecyclerViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setmOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    /**
     * item点击事件
     */
    public interface OnItemClickListener {
        /**
         * item点击事件
         *
         * @param view
         * @param position
         */
        void onItemClick(View view, int position);
    }
}
